import Decimal from 'decimal.js'

export default function () {
  /**
   * 加法
   */
  const add = (number1: string | number, number2: string | number): number => {
    const num1 = new Decimal(number1)
    const num2 = new Decimal(number2)
    const result = num1.add(num2)
    return Number(result)
  }

  /**
   * 减法
   */
  const sub = (number1: string | number, number2: string | number): number => {
    const num1 = new Decimal(number1)
    const num2 = new Decimal(number2)
    const result = num1.sub(num2)
    return Number(result)
  }

  /**
   * 乘法
   */
  const mul = (number1: string | number, number2: string | number): number => {
    const num1 = new Decimal(number1)
    const num2 = new Decimal(number2)
    const result = num1.mul(num2)
    return Number(result)
  }

  /**
   * 除法
   */
  const div = function (number1: string | number, number2: string | number): number {
    if (!number1) return 0
    if (number2 == 0) return 0
    const num1 = new Decimal(number1)
    const num2 = new Decimal(number2)
    const result = num1.div(num2)
    return Number(result)
  }

  /**
   * 加法-数组
   */
  const addList = (numList: (number | string)[]): number => {
    let result = new Decimal(0)
    for (let numItem of numList) {
      const curNum = new Decimal(numItem)
      result = result.add(curNum)
    }
    return Number(result)
  }

  /**
   * 乘法-数组
   */
  const mulList = (numList: (number | string)[]): number => {
    let result = new Decimal(1)
    for (let numItem of numList) {
      const curNum = new Decimal(numItem)
      result = result.mul(curNum)
    }
    return Number(result)
  }

  return {
    add,
    sub,
    mul,
    div,
    addList,
    mulList
  }
}
